
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ScrewTurn.Wiki.PluginFramework;

namespace ScrewTurn.Wiki {

	public partial class Zip : System.Web.UI.Page {

		protected void Page_Load(object sender, EventArgs e) {
			CreateZip();
		}

		private void CreateZip() {
			bool found = false;
			if(Request["Username"] != null && Request["Password"] != null) {
				// Try to login
				Users users = Users.Instance;
				for(int i = 0; i < users.AllUsers.Count; i++) {
					if(users.AllUsers[i].Username.Equals(Request["Username"]) &&
						users.AllUsers[i].Provider.TestAccount(users.AllUsers[i], Request["Password"]) &&
						users.AllUsers[i].Active && users.AllUsers[i].Admin) {
						// Authenticated
						found = true;
						Log.LogEntry("Backup ZIP requested (script)", EntryType.General, Request["Username"]);
						break;
					}
				}
				if(!found) {
					// Try built-in admin
					if(Request["Username"].Equals("admin") && Request["Password"].Equals(Settings.MasterPassword)) {
						found = true;
						Log.LogEntry("Backup ZIP requested (script)", EntryType.General, Request["Username"]);
					}
				}
			}

			if(!found) {
				if(SessionFacade.LoginKey == null) Response.Redirect("/Members/Login.aspx?Redirect=Zip.aspx");
				if(!SessionFacade.Admin) Response.Redirect("AccessDenied.aspx");
			}

			List<string> tmp = new List<string>();
			tmp.AddRange(Directory.GetFiles(Settings.PublicDirectory));
			tmp.AddRange(Directory.GetFiles(Settings.PagesDirectory));
			tmp.AddRange(Directory.GetFiles(Settings.MessagesDirectory));
			tmp.AddRange(Directory.GetFiles(Settings.PluginsDirectory));
			tmp.AddRange(Directory.GetFiles(Settings.PluginsConfigurationDirectory));
			tmp.AddRange(Directory.GetFiles(Settings.SnippetsDirectory));
			// Recursively add Upload subdirectories and files
			AddItems(Settings.UploadDirectory, tmp);

			string[] files = tmp.ToArray();

			int count = 0;
			if(Application["BackupCount"] == null) Application["BackupCount"] = 0;
			else Application["BackupCount"] = (int)Application["BackupCount"] + 1;
			count = (int)Application["BackupCount"];

			DateTime dt = DateTime.Now;
			string filename = "Backup-" + count.ToString() + "-" + string.Format("{0:yyyy'.'MM'.'dd'-'HH'.'mm'.'ss}.{1}", dt, dt.Millisecond) + ".zip";
			ZipFile.Create(files, Settings.PublicDirectory, Settings.TempDirectory + filename, 9);
			Log.LogEntry("Backup ZIP created", EntryType.General, "SYSTEM");
			//Response.Redirect(Settings.PublicDirectoryName + "/" + Settings.TempDirectoryName + "/" + filename);
			Response.Clear();
			FileInfo info = new FileInfo(Settings.TempDirectory + filename);
			Response.AddHeader("content-type", "application/zip");
			Response.AddHeader("content-length", info.Length.ToString());
			Response.AddHeader("content-disposition", "attachment;filename=" + filename);
			Response.WriteFile(Settings.TempDirectory + filename);
		}

		private void AddItems(string directory, List<string> list) {
			list.AddRange(Directory.GetFiles(directory));
			string[] dirs = Directory.GetDirectories(directory);
			for(int i = 0; i < dirs.Length; i++) {
				AddItems(dirs[i], list);
			}
		}

	}

}
